package com.github.niefy.modules.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.niefy.common.exception.RRException;
import com.github.niefy.modules.wx.dao.WeiboConfigMapper;
import com.github.niefy.modules.wx.dto.weiboconfig.WeiboConfigDTO;
import com.github.niefy.modules.wx.dto.weiboconfig.WeiboConfigWxAppidDTO;
import com.github.niefy.modules.wx.entity.WeiboConfig;
import com.github.niefy.modules.wx.entity.WxAccount;
import com.github.niefy.modules.wx.entity.common.CommonQuery;
import com.github.niefy.modules.wx.enums.CommonYesNoEnum;
import com.github.niefy.modules.wx.service.WeiboConfigService;
import com.github.niefy.modules.wx.service.WxAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class WeiBoConfigServiceImpl extends ServiceImpl<WeiboConfigMapper, WeiboConfig> implements WeiboConfigService {
    @Autowired
    private WxAccountService wxAccountService;

    @Override
    public Page<WeiboConfig> pageService(CommonQuery<WeiboConfig> page) {
        Page<WeiboConfig> queryPage = page.toPage();
        LambdaQueryWrapper<WeiboConfig> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(WeiboConfig::getDelFlag, CommonYesNoEnum.YES.getCode());
        Page<WeiboConfig> dbPage = super.page(queryPage, lambdaQuery);
        if (ObjectUtils.isEmpty(dbPage)) {
            return dbPage;
        }
        if (ObjectUtils.isEmpty(dbPage.getRecords())) {
            return dbPage;
        }
        List<String> wxAppIdList = dbPage.getRecords().stream().map(WeiboConfig::getWxAppId).collect(Collectors.toList());
        Map<String, WxAccount> wxAccountMap = wxAccountService.listByAppIdReturnMap(wxAppIdList);

        for (WeiboConfig record : dbPage.getRecords()) {
            String wxAppId = record.getWxAppId();
            if (wxAccountMap.containsKey(wxAppId)) {
                WxAccount wxAccount = wxAccountMap.get(wxAppId);
                record.setWxName(wxAccount.getName());
            }
        }

        return dbPage;
    }

    @Override
    public WeiboConfig saveService(WeiboConfig input) {
        String wxAppId = input.getWxAppId();
        if (StringUtils.isBlank(wxAppId)) {
            throw new RRException("wxAppId不能为空");
        }
        WeiboConfig byWxAppId = getByWxAppId(wxAppId);
        if (ObjectUtils.isNotEmpty(byWxAppId)) {
            throw new RRException("wxAppId已存在");
        }
        boolean save = super.save(input);
        return input;
    }

    @Override
    public WeiboConfig getByWxAppId(String wxAppId) {
        LambdaQueryWrapper<WeiboConfig> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(WeiboConfig::getDelFlag, CommonYesNoEnum.YES.getCode());
        lambdaQuery.eq(WeiboConfig::getWxAppId, wxAppId);
        return getOne(lambdaQuery);
    }

    @Override
    public WeiboConfig getByIdService(WeiboConfigDTO input) {
        return getById(input.getId());
    }

    public WeiboConfig getByIdService(Integer id) {
        return getById(id);
    }

    @Override
    public WeiboConfig disableService(WeiboConfigDTO input) {
        WeiboConfig byIdService = getByIdService(input);
        if (Objects.isNull(byIdService)) {
            throw new RuntimeException("数据不存在");
        }
        byIdService.setStatus(CommonYesNoEnum.NO.getCode());
        super.updateById(byIdService);
        return byIdService;
    }

    @Override
    public WeiboConfig deleteService(WeiboConfigDTO input) {
        WeiboConfig byIdService = getByIdService(input);
        if (Objects.isNull(byIdService)) {
            throw new RuntimeException("数据不存在");
        }
        byIdService.setDelFlag(CommonYesNoEnum.NO.getCode());
        super.updateById(byIdService);
        return byIdService;
    }

    @Override
    public WeiboConfig updateNormal(WeiboConfigDTO input) {
        WeiboConfig byIdService = getByIdService(input);
        if (Objects.isNull(byIdService)) {
            throw new RuntimeException("数据不存在");
        }
        byIdService.setStatus(CommonYesNoEnum.YES.getCode());
        super.updateById(byIdService);
        return byIdService;
    }

    @Override
    public WeiboConfig updateService(WeiboConfig input) {
        WeiboConfig byIdService = getByIdService(input.getId());
        if (Objects.isNull(byIdService)) {
            throw new RuntimeException("数据不存在");
        }
        super.updateById(input);
        return byIdService;
    }

    @Override
    public boolean checkCallWeiBoByNewUser(WeiboConfig input) {
        if (ObjectUtils.isEmpty(input)) {
            return false;
        }
        String status = input.getStatus();
        if (Objects.equals(CommonYesNoEnum.NO.getCode(), status)) {
            return false;
        }
        CommonYesNoEnum callWeiBoFlag = input.getCallWeiBoFlag();
        if (Objects.equals(CommonYesNoEnum.NO, callWeiBoFlag)) {
            return false;
        }

        return true;
    }
}
